(in-package :procemu)

(defparameter *next-cell-pointer* 0
  "Location of the next free area in memory")

(defun allocate-byte ()
  (prog1 *next-cell-pointer*
    (incf *next-cell-pointer*)))

(defparameter *variables* (make-hash-table))

(defun set-variable (symbol value-location)
  (let ((address (allocate-byte)))
    (setf (gethash symbol *variables*) address)
    (eval `(with-asm ()
	    (:mov (memory ,address) ,value-location)))))

(defun load-variable (symbol location)
  (let ((address (gethash symbol *variables*)))
    (eval `(with-asm ()
	    (:mov ,location (memory ,address))))))

(defun compile-arithmetic-expression (expr register)
  ;; REGISTER should be somethign like 'r0
  (typecase expr
    (number (eval `(with-asm ()
		    (:mov ,register ,expr))))
    (symbol (load-variable expr register))
    (list (destructuring-bind (operation x y) expr
	    ()
	    (eval `(with-asm ()
		    ))))))

(registers (with-asm (:execute t)
             (:mov r1 38)
             (:mov r2 2)
             (:add r1 r2)
             (:mov r0 r1)
             (:add r0 r2)))

(subseq (memory-bank (with-asm (:execute t)
		       (set-variable 'x 38)
		       (set-variable 'y 2)
		       (load-variable 'x 'r1)
		       (load-variable 'y 'r2)
		       (:add r1 r2)
		       (:mov r0 r1)
		       (:add r0 r2)
		       (set-variable 'z 'r0))) 0 10)

(with-asm ()
  (set-variable 'x 38)
  (set-variable 'y 2)
  (load-variable 'x 'r1)
  (load-variable 'y 'r2)
  (:add r1 r2)
  (:mov r0 r1)
  (:add r0 r2)
  (set-variable 'z 'r0))